using System;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.ViewModel;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public abstract class ViewModelBase : NotificationObject, INavigationAware
    {
        #region Implementation of INavigationAware

        public virtual void OnNavigate(object extraData)
        {
            //will be implemented in the sub classes
        }

        public virtual void OnLeave()
        {
            //will be implemented in the sub classes
        }

        #endregion

        #region Binding Properties

        #region Message

        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                if (!ReferenceEquals(message, value))
                {
                    message = value;
                    RaisePropertyChanged(() => Message);
                }
            }
        }

        private Visibility messageVisibility;
        public Visibility MessageVisibility
        {
            get { return messageVisibility; }
            set
            {
                if (!Equals(messageVisibility, value))
                {
                    messageVisibility = value;
                    RaisePropertyChanged(() => MessageVisibility);
                }
            }
        }

        private Visibility errorMessageVisibility;
        public Visibility ErrorMessageVisibility
        {
            get { return errorMessageVisibility; }
            set
            {
                if (!Equals(errorMessageVisibility, value))
                {
                    errorMessageVisibility = value;
                    RaisePropertyChanged(() => ErrorMessageVisibility);
                }
            }
        }

        #endregion

        private int serviceCallCounter;
        public int ServiceCallCounter
        {
            get { return serviceCallCounter; }
            set
            {
                if (serviceCallCounter != value)
                {
                    serviceCallCounter = value;
                    RaisePropertyChanged(() => ServiceCallCounter);
                    if (serviceCallCounter == 0)
                    {
                        HandleLoadingCompleted();
                    }
                }
            }
        }

        private bool isShowAll;
        public bool IsShowAll
        {
            get { return isShowAll; }
            set
            {
                if (!Equals(isShowAll, value))
                {
                    isShowAll = value;
                    RaisePropertyChanged(()=>IsShowAll);
                }
            }
        }

        public Account CurrentUser
        {
            get { return IMSIsolatedStorage.CurrentUser; }
        }

        //public bool CanView { 
        //    get
        //    {
        //        return CurrentUser.CanViewPartner || CurrentUser.CanViewEmployee || CurrentUser.CanViewEmploymentType
        //               || CurrentUser.CanViewMaterial || CurrentUser.CanViewOrderMaterial
        //               || CurrentUser.CanViewOrderProduct || CurrentUser.CanViewProduct || CurrentUser.CanViewRole ||
        //               CurrentUser.CanViewWarehouse;
        //    }
        //}

        private bool canEditOrAdd;
        public bool CanEditOrAdd
        {
            get { return canEditOrAdd; }
            set
            {
                if (!Equals(canEditOrAdd,value))
                {
                    canEditOrAdd = value;
                    RaisePropertyChanged(()=>CanEditOrAdd);
                }
            }
        }

        private bool canAdd;
        public bool CanAdd
        {
            get { return canAdd; }
            set
            {
                if (!Equals(canAdd, value))
                {
                    canAdd = value;
                    RaisePropertyChanged(() => CanAdd);
                }
            }
        }

        #endregion
        
        #region Fields

        protected bool IsFirstLoaded;

        protected ModelBase Model;

        protected NavigationData NavigationData;

        #endregion

        #region Support Methods

        protected virtual void HandleLoadingCompleted()
        {
            
        }

        protected void SetMessageVisibility(bool  isSuccessful)
        {
            if (isSuccessful)
            {
                MessageVisibility = Visibility.Visible;
                ErrorMessageVisibility = Visibility.Collapsed;
            }
            else
            {
                MessageVisibility = Visibility.Collapsed;
                ErrorMessageVisibility = Visibility.Visible;
            }
        }

        #endregion

    }
}